package inteface;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.WindowConstants;
import javax.swing.table.AbstractTableModel;

import divers.Informations;

import algorithmes.Echantillon;
import algorithmes.Instance;

public class FenetreDonnees extends JFrame {
	/**
	 * SerialVersionUID.
	 */
	private static final long serialVersionUID = 1L;
	private static final boolean DEBUG = true;
	private Vector<Vector> data;
	private Vector<Vector> dataTest;
	private Vector columnNames;
	private Vector columnNamesTest;

	public FenetreDonnees(String title, Echantillon e, Informations infos) throws HeadlessException {
		super(title);
		getContentPane().setLayout(new BorderLayout());
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		JButton OK = new JButton("Valider");
		JButton tg = new JButton("<-");
		JButton td = new JButton("->");
		//GridLayout gl = new GridLayout(1,3);
		//gl.setHgap(10);
		JPanel phaut = new JPanel(new BorderLayout());
		JPanel pmilieu = new JPanel(new FlowLayout());
		pmilieu.add(tg);
		pmilieu.add(td);
		JPanel pbas = new JPanel(new BorderLayout());
		JPanel pbas2 = new JPanel();
		pbas.add(pbas2, BorderLayout.CENTER);
		
		data = new Vector<Vector>();
		for (Instance i : e.getvInstances())
			data.add(i.getValeurs());
		dataTest = new Vector<Vector>();
		for (Instance i : e.getvInstances())
			dataTest.add(i.getValeurs());
		columnNames = infos.getNomAttributs();
		columnNamesTest = infos.getNomAttributs();
		
		final MyTableModel mytm1 = new MyTableModel(columnNames, data);
		JTable donnees = new JTable(mytm1);
		donnees.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		donnees.setPreferredScrollableViewportSize(new Dimension(370,200));
		
		final MyTableModel mytm2 = new MyTableModel(columnNamesTest, dataTest);
		JTable donneesTest = new JTable(mytm2);
		donneesTest.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		donneesTest.setPreferredScrollableViewportSize(new Dimension(370,200));
		
		JScrollPane scrollPane = new JScrollPane(donnees);
		scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		
		JScrollPane scrollPaneTest = new JScrollPane(donneesTest);
		scrollPaneTest.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		scrollPaneTest.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		//pgauche.add(scrollPane);
		//pdroite.add(scrollPaneTest);
		pbas2.add(OK);
		
		ActionListener al = new ActionListener() {
			public void actionPerformed(ActionEvent eap) {
				dispose();
			}
		};
		OK.addActionListener(al);
		
		phaut.add(scrollPane, BorderLayout.WEST);
		phaut.add(pmilieu, BorderLayout.CENTER);
		phaut.add(scrollPaneTest, BorderLayout.EAST);
		pmilieu.setSize(new Dimension(100,100));
		getContentPane().add(phaut, BorderLayout.CENTER);
		getContentPane().add(pbas, BorderLayout.SOUTH);
		
		setPreferredSize(new Dimension(800, 300));
		phaut.setPreferredSize(new Dimension(800, 200));
		//scrollPane.setPreferredSize(new Dimension(370, 200));
		pmilieu.setSize(new Dimension(60, 200));
		//scrollPaneTest.setPreferredSize(new Dimension(370, 200));
		pbas.setPreferredSize(new Dimension(800, 40));
		//pbas2.setPreferredSize(new Dimension(100, 100));
		
	   	pack();
	   	setVisible(true);
	   	
	    if (DEBUG) {
	    	donnees.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent eap) {
                    //mytm1.printDebugData();
                }
            });
	    	
	    	donneesTest.addMouseListener(new MouseAdapter() {
                public void mouseClicked(MouseEvent eap) {
                   // mytm2.printDebugData();
                }
            });
        }
	}
	
	 class MyTableModel extends AbstractTableModel {
		 /**
		 * SerialVersionUID.
		 */
		private static final long serialVersionUID = 1L;
		private Vector<Vector> donnees;
		 private Vector colonnes;

		 public MyTableModel(Vector columnNames, Vector<Vector> data) {
			 colonnes = columnNames;
			 donnees = data; 
		 }
	        public int getColumnCount() {
	            return colonnes.size();
	        }

	        public int getRowCount() {
	            return donnees.size();
	        }

	        public String getColumnName(int col) {
	            return colonnes.get(col).toString();
	        }

	        public Object getValueAt(int row, int col) {
	            return donnees.get(row).get(col);
	        }

	        /*
	         * JTable uses this method to determine the default renderer/
	         * editor for each cell.  If we didn't implement this method,
	         * then the last column would contain text ("true"/"false"),
	         * rather than a check box.
	         */
	        @SuppressWarnings("unchecked")
			public Class getColumnClass(int c) {
	            return getValueAt(0, c).getClass();
	        }

	        /*
	         * Don't need to implement this method unless your table's
	         * editable.
	         */
	        public boolean isCellEditable(int row, int col) {
	        	return true;
	        }

	        /*
	         * Don't need to implement this method unless your table's
	         * data can change.
	         */
	        @SuppressWarnings("unchecked")
			public void setValueAt(Object value, int row, int col) {
	            if (DEBUG) {
	                System.out.println("Setting value at " + row + "," + col
	                                   + " to " + value
	                                   + " (an instance of "
	                                   + value.getClass() + ")");
	            }

	            donnees.get(row).set(col,value);
	            fireTableCellUpdated(row, col);

	            if (DEBUG) {
	             //   System.out.println("New value of data:");
	            }
	        }

	        @SuppressWarnings("unused")
			private void printDebugData() {
	            int numRows = getRowCount();
	            int numCols = getColumnCount();

	            for (int i=0; i < numRows; i++) {
	                System.out.print("    row " + i + ":");
	                for (int j=0; j < numCols; j++) {
	                    System.out.print("  " + donnees.get(i).get(j));
	                }
	                System.out.println();
	            }
	            System.out.println("--------------------------");
	        }
	    }
}
